package ofc4j.util;

import ofc4j.model.elements.HorizontalBarChart;
import ofc4j.model.elements.HorizontalBarChart.Bar;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.path.PathTrackingWriter;

public class HorizontalBarChartBarConverter implements Converter {
    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert(Class c) {
        return HorizontalBarChart.Bar.class.isAssignableFrom(c);
    }

    @Override
    public void marshal(Object o, HierarchicalStreamWriter hsw, MarshallingContext ctxt) {
        PathTrackingWriter writer = (PathTrackingWriter) hsw;
        Bar b = (Bar) o;
        writer.startNode("right", b.getRight().getClass());
        writer.setValue(b.getRight().toString());
        writer.endNode();
        Number l = b.getLeft();
        if (l != null) {
            writer.startNode("left", b.getLeft().getClass());
            writer.setValue(l.toString());
            writer.endNode();
        }
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {
        return null;
    }
}
